use core::arch::asm;

use super::task::Task;
use crate::boot::NR_CPUS;

#[inline(always)]
pub fn current() -> &'static Task {
    cfg_if::cfg_if! {
        if #[cfg(target_arch = "aarch64")] {
            unsafe {
                let task: usize;
                asm!(
                    "mrs {0}, sp_el0",
                    out(reg) task
                );
                &*(task as *const Task)
            }
        } else {
            unreachable!()
        }
    }
}

#[inline(always)]
pub fn current_mut() -> &'static mut Task {
    cfg_if::cfg_if! {
        if #[cfg(target_arch = "aarch64")] {
            unsafe {
                let task: usize;
                asm!(
                    "mrs {0}, sp_el0",
                    out(reg) task
                );
                &mut *(task as *mut Task)
            }
        } else {
            unreachable!()
        }
    }
}

#[no_mangle]
pub static PERCPU_ENTRY_TASK: [usize; NR_CPUS] = [0; NR_CPUS];
